<?php
// include function files for this application
require_once('/home/domains/uny-con.com/public_html/uny-con_fns.php');
if (!session_id()) { session_start(); }

   do_html_header("UNY-Con Create text file of events", staysmall, $uny);

    //connect to the DB
   if (!($conn = db_connect()))
     return false;

$prev_event_array[] = "";

$filename = 'allevents.csv';
$filename2 = 'quicksched.txt';
//fopen ($filename, 'w+');
if (is_writable($filename)) {
	// open file as writeable and zero it.
	if (!$fp = fopen($filename, 'w+b')) {
		print "<span class=\"redtext\">Cannot open file ($filename)</span>";
		exit;
		}
	//print "Success, wrote ($someconent entries) to file ($filename)";
	fclose($fp);
	} # close iswriteable if
	else {
	echo "<span class=\"redtext\">File is not writeable!</span>";
	}
if (is_writable($filename2)) {
	// open file as writeable and zero it.
	if (!$fp = fopen($filename2, 'w+b')) {
		print "<span class=\"redtext\">Cannot open file ($filename2)</span>";
		}
	fclose($fp);
	} # close iswriteable if
	else {
	echo "<span class=\"redtext\">File $filename2 is not writeable!</span>";
	}


$result = get_event_list();
$num = pg_numrows($result);
$i=0;
while ($i < $num) {
	$values = recordproc($result, $i);
	extract($values);
	//This checks to see what timeslot the current record is in. If we have moved to a new timeslot from the previous record we clos the previous table and start a new one, otherwise we write another table cell.
	$current_ts = $starting_timeslot;
	if ($working_ts != $current_ts) {
		$working_ts = $current_ts;
		$tstime = print_timeslot($current_ts);
//		echo "$tstime\n";
		$somecontent = "\n$tstime";
		write_to_file($somecontent, $filename);
		$quickcontent = "\n$tstime";
		write_to_file($quickcontent, $filename2);
		}
	$current_event_type = $event_type;
	if ($working_event_type != $current_event_type) {
		$working_event_type = $current_event_type;
		$somecontent = "\n$current_event_type\nEvent Title,System,GM,Location,Event Code";
		write_to_file($somecontent, $filename);
		}
		unset($processed_numts, $processed_equip, $processed_event_id, $processed_maturity, $print_description, $mature_superscript, $numts_superscript, $cost_superscript, $processed_cost);
		//this gets rid of mulitple \n characters.
		$print_description = str_replace(" \n" , "\n", $description);
		$print_description = str_replace("\n\n" , "", $description);
		$print_description = str_replace("," , ";comma;", $print_description);
		$event_title = str_replace("," , ";comma;", $event_title);
		$game_system = str_replace("," , ";comma;", $game_system);
		if ($num_of_timeslots != "1") {
			$processed_event_id = "TS$starting_timeslot"."$event_id*";
			} else {
			$processed_event_id = "TS$starting_timeslot"."$event_id";
			}
		if ($num_of_timeslots != "1" && $num_of_timeslots != "2") {
			$processed_event_id = "TS$starting_timeslot"."$event_id*";
			$processed_numts = " *Note, this event lasts $num_of_timeslots timeslots.";
			}
		if ($num_of_timeslots == "2") {
			$numts_superscript = " $5$";
			}
		if ($equipment != "") {
			$processed_equip = " Attendees should bring: $equipment.";
			}
		if ($maturity == "M") {
			$processed_maturity = " This event may contain mature themes.";
			$mature_superscript = "$1$";
			}
		if ($maturity == "Y") {
			$processed_maturity = " This event will contain mature themes.";
			$mature_superscript = "$1$";
			}
		if ($cost == "1.00") {
			$cost_superscript = " $2$";
			}
		if ($cost == "2.00") {
			$cost_superscript = " $3$";
			}
		if ($cost == "4.00") {
			$cost_superscript = " $4$";
			}
		if ($cost == "3.00" || $cost > "4") {
			$processed_cost = " This event has an additional cost of $$cost.";
			}
		if (array_key_exists($event_id, $prev_event_array)) {
			$print_description = "Please see event description for event TS".$prev_event_array["$event_id"]."$event_id in timeslot ".$prev_event_array["$event_id"].".";
			unset($processed_equip, $processed_maturity, $processed_cost);
			}
		$somecontent = "\n$event_title$mature_superscript$cost_superscript$numts_superscript,$game_system,$name,$assigned_room,$processed_event_id
$print_description"."$processed_cost"."$processed_equip"."$processed_numts";
		write_to_file($somecontent, $filename);
		$processed_game_system = str_replace("Dungeons & Dragons","D&D","$game_system");
		$quickcontent = "\n$processed_event_id&&&$processed_game_system";
		write_to_file($quickcontent, $filename2);
		if (!array_key_exists($event_id, $prev_event_array)) $prev_event_array["$event_id"] = "$starting_timeslot";
		unset($somecontent, $quickcontent);
	$i++;
	}

do_html_URL("$filename", "Download $filename");
print_brs(1);
do_html_URL("$filename2", "Download $filename2");
print_brs(24);
do_html_footer(news);